package com.dlwx.firefighting.fragment.order;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.activity.OrderDetailsActivity;
import com.dlwx.firefighting.adapter.UserOrderAdapter;
import com.dlwx.firefighting.bean.UserOrderBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DealResult;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 用户订单fragment
 */
public class UserOrderFragment extends Fragment {
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view3)
    View view3;
    Unbinder unbinder;
    @BindView(R.id.tv_order_yishouhuo)
    TextView tvOrderYishouhuo;

    @BindView(R.id.tv_order_weishouhuo)
    TextView tvOrderWeishouhuo;
    @BindView(R.id.tv_order_all)
    TextView tvOrderAll;
    private int loadType = 0;
    private LRecyclerView recyclerView = null;
    private UserOrderAdapter adapter = null;
    private LRecyclerViewAdapter lRecyclerViewAdapter = null;
    private AutoLinearLayout emptyView;
    private int page = 1;
    private int requestNum = 10;
    private int count = 0;
    private String userId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_order, container, false);
        Bundle bundle = getArguments();
        if (bundle != null) {
            loadType = bundle.getInt("loadType");
        }
        loadType = 1;
        emptyView = (AutoLinearLayout) view.findViewById(R.id.order_empty_view);
        recyclerView = (LRecyclerView) view.findViewById(R.id.user_order_recycler);
        userId = (String) SPUtils.get(getActivity(), Constant.USER_ID, "");
        initRecy();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

    private void getDatas() {
//        1未付款 2待发货 3待收货4全部 5 已收货
        OkGo.<String>post(Constant.ORDER_LIST)
                .tag(this)
                .params("uid", userId)
                .params("orderType", loadType)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DealResult dealResult = new DealResult();
                        List<UserOrderBean> beans = new ArrayList<>();
                        beans.add(new UserOrderBean());
                        beans.add(new UserOrderBean());
                        beans.add(new UserOrderBean());
                        beans.add(new UserOrderBean());
                        adapter.addAll(beans);
                        recyclerView.refreshComplete(requestNum);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void initRecy() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new UserOrderAdapter(getActivity(), loadType);
        adapter.setDataList(new ArrayList<UserOrderBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.size_5dp)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.app_bg_color)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        //设置下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        //设置上拉加载更多
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (count < (page * requestNum)) {
                    ToastUtils.toastForShort(getActivity(), getString(R.string.no_more));
                    recyclerView.setNoMore(true);
                } else {
                    page++;
                    getDatas();

                }
            }
        });
        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.fuzhu_black, R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.fuzhu_black, R.color.white);
        //设置底部加载文字提示
        recyclerView.setFooterViewHint("加载中", "已经到底了", "网络不给力啊，点击再试一次吧");
        // lRecyclerViewAdapter.removeFooterView();
        //初始化数据
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                UserOrderBean bean = adapter.getDataList().get(position);
                if (TextUtils.equals(bean.getIs_online(), "1")) {
                    return;
                }
                Intent intent = new Intent(getActivity(), OrderDetailsActivity.class);
                intent.putExtra("id", bean.getOrder_id());
                intent.putExtra("loadType", loadType);
                startActivity(intent);
            }
        });

    }

    public void notifyView() {
        recyclerView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
    }

    /**
     * 刷新
     */
    private void refresh() {
        adapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        page = 1;
        getDatas();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_order_yishouhuo, R.id.tv_order_weishouhuo,R.id.tv_order_all})
    public void onViewClicked(View view) {
        initTitle();
        int red = ContextCompat.getColor(getActivity(), R.color.colorAccent);
        int text_grey = ContextCompat.getColor(getActivity(), R.color.all_textcolor);
        int app_bg_color = ContextCompat.getColor(getActivity(), R.color.app_bg_color);
        switch (view.getId()) {
            case R.id.tv_order_yishouhuo:
                tvOrderYishouhuo.setTextColor(red);
                view2.setBackgroundColor(red);
                break;
            case R.id.tv_order_weishouhuo:
                tvOrderWeishouhuo.setTextColor(red);
                view3.setBackgroundColor(red);
                break;
            case R.id.tv_order_all:
                tvOrderAll.setTextColor(red);
                view1.setBackgroundColor(red);
                break;
        }
    }

    public void initTitle(){
        int red = ContextCompat.getColor(getActivity(), R.color.colorAccent);
        int text_grey = ContextCompat.getColor(getActivity(), R.color.all_textcolor);
        int app_bg_color = ContextCompat.getColor(getActivity(), R.color.app_bg_color);
        tvOrderWeishouhuo.setTextColor(text_grey);
        tvOrderYishouhuo.setTextColor(text_grey);
        tvOrderAll.setTextColor(text_grey);
        view1.setBackgroundColor(app_bg_color);
        view2.setBackgroundColor(app_bg_color);
        view3.setBackgroundColor(app_bg_color);
    }


    //-------------------------------------------------------------
}
